package com.message.api.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.context.Context;
import com.base.helper.StringUtils;
import com.message.api.group.dao.AcGroupActionMapper;
import com.message.api.group.entity.AcGroup;
import com.message.api.group.entity.AcGroupAction;
import com.message.api.group.service.AcGroupActionService;
import com.message.api.group.service.AcGroupService;
import com.message.api.group.vo.PurviewGrantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcGroupActionServiceImpl extends ServiceImpl<AcGroupActionMapper, AcGroupAction> implements AcGroupActionService {

    @Autowired
    private AcGroupService acGroupService;

    @Override
    public List<AcGroupAction> list(AcGroupAction example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<AcGroupAction> page(AcGroupAction example, IPage page) {
        return this.page(page, buildWrapper(example));
    }

    @Override
    public List<AcGroupAction> findByGroupid(Integer groupid) {
        AcGroup acGroup = acGroupService.getById(groupid);
        if (null == acGroup || "0".equals(acGroup.getState())) {
            throw new RuntimeException("没有找到id对应角色");
        }
        QueryWrapper<AcGroupAction> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AcGroupAction::getAcgroup, acGroup.getAcgroup())
                .eq(AcGroupAction::getState, "1");
        return this.list(wrapper);
    }

    @Override
    public void grantPurviews(Context context, PurviewGrantVo purviewGrant) {
        AcGroup acGroup = acGroupService.getById(purviewGrant.getGroupid());
        if (null == acGroup || "0".equals(acGroup.getState())) {
            throw new RuntimeException("没有找到id对应角色");
        }
        List<AcGroupAction> groupActions = this.findByGroupid(acGroup.getId());
        List<String> oldActions =
                groupActions.stream().map(AcGroupAction::getAction).collect(Collectors.toList());
        List<String> newActions = purviewGrant.getActions();
        List<String> removeActions =
                oldActions.stream().filter(e -> !newActions.contains(e)).collect(Collectors.toList());
        List<String> addActions =
                newActions.stream().filter(e -> !oldActions.contains(e)).collect(Collectors.toList());
        List<AcGroupAction> removes =
                groupActions.stream().filter(e -> removeActions.contains(e.getAction())).collect(Collectors.toList());
        removes.forEach(e -> {
            e.setState("0");
            e.setDeloperator(null != context.currentUser() ? context.currentUser().getId() : null);
        });
        if (null != removes && removes.size() > 0) {
            this.updateBatchById(removes);
        }
        List<AcGroupAction> adds = addActions.stream().map(e -> {
            AcGroupAction acGroupAction = new AcGroupAction();
            acGroupAction.setAcgroup(acGroup.getAcgroup());
            acGroupAction.setAction(e);
            acGroupAction.setAddoperator(null != context.currentUser() ? context.currentUser().getId() : null);
            acGroupAction.setCreatetime(new Date());
            acGroupAction.setState("1");
            return acGroupAction;
        }).collect(Collectors.toList());
        if (null != adds && adds.size() > 0) {
            this.saveBatch(adds);
        }
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<AcGroupAction> buildWrapper(AcGroupAction example) {
        QueryWrapper<AcGroupAction> wrapper     = new QueryWrapper<>();
        String                      acgroup     = example.getAcgroup();
        String                      action      = example.getAction();
        Integer                     addoperator = example.getAddoperator();
        Integer                     deloperator = example.getDeloperator();

        wrapper.lambda()
                .like(StringUtils.isNotEmpty(acgroup), AcGroupAction::getAcgroup, acgroup)
                .like(StringUtils.isNotEmpty(action), AcGroupAction::getAction, action)
                .eq(null != addoperator, AcGroupAction::getAddoperator, addoperator)
                .eq(null != deloperator, AcGroupAction::getDeloperator, deloperator)
                .eq(AcGroupAction::getState, "1")
                .orderByAsc(AcGroupAction::getId);
        return wrapper;
    }

}
